import io
import logging
from UserInterface.MyLogger import *

class StringLogger(MyLogger):
    def __init__(self, level:LogLevel):
        super().__init__(level)
        self.logger = logging.getLogger('default')
        defaultFormat = '[%(asctime)s] [%(levelname)s]: %(message)s'
        self.defaultFormatter = logging.Formatter(defaultFormat)
        self.logStream = io.StringIO()
        self.defHandler = logging.StreamHandler(self.logStream)
        self.defHandler.setFormatter(self.defaultFormatter)
        self.defHandler.setLevel(level.value)
        self.logger.addHandler(self.defHandler)
        self.SetOutLevel(level)
        blankFormat = '%(message)s'
        self.blankFormatter = logging.Formatter(blankFormat)

    def Debug(self, content:str):
        self.logger.debug(content)
    
    def Info(self, content:str):
        self.logger.info(content)

    def Warning(self, content:str):
        self.logger.warning(content)

    def Error(self, content:str):
        self.logger.error(content)

    def print(self, content:str):
        if not self.defHandler:
            return
        self.defHandler.setFormatter(self.blankFormatter)
        self.logger.error(content)
        self.defHandler.setFormatter(self.defaultFormatter)

    def SetOutLevel(self, level:LogLevel):
        self.outLevel = level
        self.logger.setLevel(level.value)

    def GetLogContent(self) -> str:
        return self.logStream.getvalue()
    
    def ClearBuffer(self) -> None:
        self.logStream.seek(0)
        self.logStream.truncate(0)